//
//  MDNetDetail.m
//  SmartBusiness
//
//  Created by 梦居 on 2018/9/4.
//  Copyright © 2018年 MD. All rights reserved.
//

#import "MDNetDetail.h"
#import "MDNet.h"
#import "AFNetworkReachabilityManager.h"
#import "AFHTTPRequestOperationManager.h"
#import <CommonCrypto/CommonDigest.h>
@implementation MDNetDetail
+ (void)GETNet:(NSString *)interface param:(NSDictionary *)param block:(void (^)(id posts, NSInteger code ,NSString *Meg))block
{
    NSMutableString *muStr = [[NSMutableString alloc] initWithString:interface];
    
    if(param)
    {
        for(NSString *key in param.allKeys)
        {
            [muStr appendString:[NSString stringWithFormat:@"%@%@",key,param[key]]];
        }
    }
    [self dealWithGet:interface param:param block:^(id posts, NSInteger code,NSString *Meg, id JSON) {
        
        if(block) {
            block(posts,code,Meg);
        }
    }];
    
}

+ (void)dealWithGet:(NSString*)interface
              param:(NSDictionary*)param
              block:(void (^)(NSDictionary * posts, NSInteger code,NSString *Meg, id josn))block {
    NSMutableDictionary *dicParam = [[NSMutableDictionary alloc] initWithDictionary:param];
    interface = [NSString stringWithFormat:@"%@",interface];
    [[MDNet shareInit] GET:interface parameters:dicParam progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@",responseObject);
        NSDictionary * JSON = responseObject;
        NSString *status=[NSString stringWithFormat:@"%@",JSON[@"code"]];
        NSString *Message=[NSString stringWithFormat:@"%@",JSON[@"msg"]];
        
        if ([status integerValue]==0) {
            if (block)
            {
                block(JSON, [status integerValue],Message,nil);
            }
        }
        else
        {
            if (block)
            {
                block(nil, [status integerValue],Message,nil);
            }
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [MBProgressHUD hideHUD];
        [MBProgressHUD showErrorMessage:@"服务器无响应"];
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        manager.requestSerializer = [AFJSONRequestSerializer serializer];
        NSLog ( @"manager.requestSerializer: %@" , manager.requestSerializer.HTTPRequestHeaders );
        if (block) {
            NSLog(@"网络连接错误t%@",error.localizedDescription);
            //error.localizedDescription
        }
    }];
}






+ (void)POSTNet:(NSString*)interface
          param:(NSDictionary*)param
          block:(void (^)(id posts, NSInteger code ,NSString *Meg))block
{
    
    NSMutableString *muStr = [[NSMutableString alloc] initWithString:interface];
    
    if(param)
    {
        for(NSString *key in param.allKeys)
        {
            [muStr appendString:[NSString stringWithFormat:@"%@%@",key,param[key]]];
        }
    }
    [self dealWithNet:interface param:param block:^(id posts, NSInteger code,NSString *Meg, id JSON) {
        
        if(block) {
            block(posts,code,Meg);
        }
    }];
}

+ (void)dealWithNet:(NSString*)interface
              param:(NSDictionary*)param
              block:(void (^)(NSDictionary * posts, NSInteger code,NSString *Meg, id josn))block {
    NSMutableDictionary *dicParam = [[NSMutableDictionary alloc] initWithDictionary:param];
    interface = [NSString stringWithFormat:@"%@",interface];
    [[MDNet shareInit] POST:interface parameters:[self getNewParam:dicParam] progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject)
     {
         NSDictionary * JSON = responseObject;
         NSString *status=[NSString stringWithFormat:@"%@",JSON[@"code"]];
         NSString *Message=[NSString stringWithFormat:@"%@",JSON[@"msg"]];
         
         if ([status integerValue]==0) {
             if (block)
             {
                 block(JSON, [status integerValue],Message,nil);
             }
         }
         else
         {
             if (block)
             {
                 block(nil, [status integerValue],Message,nil);
             }
         }
     } failure:^(NSURLSessionDataTask *__unused task, NSError *error)
     {
         [MBProgressHUD hideHUD];
         [MBProgressHUD showErrorMessage:@"服务器无响应"];
         AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
         manager.requestSerializer = [AFJSONRequestSerializer serializer];
         NSLog ( @"manager.requestSerializer: %@" , manager.requestSerializer.HTTPRequestHeaders );
         if (block) {
             NSLog(@"网络连接错误t%@",error.localizedDescription);
             //error.localizedDescription
         }
     }];
}

+ (NSMutableDictionary*)getNewParam:(NSMutableDictionary*)dicParam {
    NSMutableString *muStr = [[NSMutableString alloc] init];
    
    for (NSInteger i=0; i<dicParam.allKeys.count; i++ ) {
        
        NSString *key = dicParam.allKeys[i];
        
        if ([dicParam[key] isKindOfClass:[NSArray class]]) {
            for (NSString *str in dicParam[key]) {
                [muStr appendString:[NSString stringWithFormat:@"%@",str]];
            }
        } else {
            
            [muStr appendString:[NSString stringWithFormat:@"%@",dicParam[key]]];
        }
    }
    //    NSString *sign = [self getMd5_32Bit_String:[self getSha1String:[NSString stringWithFormat:@"%lu",(unsigned long)muStr.length]]];
    //    sign = [NSString stringWithFormat:@"%@%@",[sign substringWithRange:NSMakeRange(0, 8)],[sign substringWithRange:NSMakeRange(sign.length - 8, 8)]];
    //    [dicParam setObject:sign forKey:@"sign"];
    return dicParam;
}
@end
